﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Configuration;
using KPIS.GERP.ESS.BLL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class SelectSystem : System.Web.UI.Page
    {
        static string strServerURL;
        private B_HrmEmployees emp = new B_HrmEmployees();
        private B_SECURITY_Users userBLL = new B_SECURITY_Users();
        //static List<M_FIM_ReceiveDtls> dtlsInfoList;
        //SetGFMS.SetGFMS setGFMS = new SetGFMS.SetGFMS();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                strServerURL = Request.Url.Host;
                HttpCookie cookie = Request.Cookies["UserDetail"];
                if (cookie != null && Session["EmpSeq"] == null)
                {
                    int nEmpSeq = -1;
                    string strEmpSeq = cookie["EmpSeq"];

                    if (strEmpSeq != null)
                    {
                        if (strEmpSeq.Length > 0)
                        {
                            nEmpSeq = Convert.ToInt32(strEmpSeq);
                        }
                    }

                    if (nEmpSeq > -1)
                    {
                        System.Data.DataRow dr = userBLL.GetUserInfo(nEmpSeq).Rows[0];
                        Session["EmpSeq"] = Convert.ToString(nEmpSeq);
                        Session["UserName"] = emp.GetEmployeeFullName(nEmpSeq);
                        Session["UserSeq"] = cookie["UserSeq"];                         
                        Session["WorkingJobSiteSeq"] = dr["working_job_site_seq"].ToString();
                        Session["WorkingJobSiteName"] = dr["working_job_site_name"].ToString();
                        Session["WorkAreaSeq"] = dr["work_area_seq"].ToString();
                        Session["Height"] = Session["Height"] ?? "550";
                    }
                }

                if (Session["UserSeq"] != null)
                {
                    //CheckPermission("ESS", PanelESS); // Everyone
                    CheckPermission("GHRMS", PanelGHRMS);
                    //CheckPermission("GBMS", PanelGBMS); // Everyone, hardCode limited some page
                    //CheckPermission("GIMS", PanelGIMS); // Xml file base, may need to move to database later
                    CheckPermission("GFMS", PanelGFMS);
                    CheckPermission("GAMS", PanelGAMS);
                    CheckPermission("ADMIN", PanelADMIN);

                    PanelAPPROVAL.Visible = (Session["UserSeq"].ToString() == "1" || Session["UserSeq"].ToString() == "24"); //เห็นเฉพาะ Admin เท่านั้น กับ คุณวัชรี
                    PanelRollback.Visible = Session["UserSeq"].ToString() == "1";
                    // CheckPermission("APPROVAL", PanelAPPROVAL); 
                }
            }
        }

        protected void CheckPermission(string strSystem, Panel cmd)
        {
            bool bOK = userBLL.GetMenuItemList(Session["UserSeq"].ToString(), strSystem).Rows.Count > 0;

            cmd.Visible = bOK;
        }

        protected void RedirectSystem(string strSystem)
        {
            if (strSystem == "ESS")
            {
                Response.Redirect(@"~/App_UI/Forms/WebForm2.aspx", true);
                return;
            }
            else if (strSystem == "Rollback")
            {
                Response.Redirect(@"~/App_UI/Forms/RollbackTransection.aspx");
                return;
            }

            string strURL;

            #region now we used shared by cookie, no need to set cookie here, we already set in ess master page
            //HttpCookie cookie = Request.Cookies["UserDetail"];
            //if (cookie == null)
            //{
            //    cookie["EmpSeq"] = ssMng.EmpSeq;
            //    cookie["UserName"] = ssMng.UserName;
            //    cookie["UserSeq"] = ssMng.UserSeq;
            //    cookie["WorkingJobSiteSeq"] = ssMng.WorkingJobSiteSeq;
            //    cookie["Height"] = ssMng.Height;
            //    cookie["TokenKey"] = ssMng.TokenKey;
            //    cookie["TokenUserTempData"] = ssMng.TokenUserTempData;

            //Session["EmpSeq"] = cookie["EmpSeq"];
            //Session["UserName"] = cookie["UserName"];
            //Session["UserSeq"] = cookie["UserSeq"];
            //Session["WorkingJobSiteSeq"] = cookie["WorkingJobSiteSeq"];
            //Session["Height"] = cookie["Height"];
            //ssMng.SetLoginSession(cookie["UserSeq"]
            //    , cookie["EmpSeq"]
            //    , cookie["UserName"]
            //    , cookie["WorkingJobSiteSeq"]
            //    , cookie["Height"]);
            //}

            //new SsMng().SetLoginSession(
            //    Session["UserSeq"].ToString(),
            //    Session["EmpSeq"].ToString(),
            //    Session["UserName"].ToString(),
            //    Session["WorkingJobSiteSeq"].ToString(),
            //    Session["Height"].ToString(),
            //    Session["TokenKey"] as string,
            //    Session["TokenUserTempData"] as string,
            //    false);

            //new SsMng().SetLoginSession(
            //    ssMng.UserSeq,
            //    ssMng.EmpSeq,
            //    ssMng.UserName,
            //    ssMng.WorkingJobSiteSeq,
            //    ssMng.Height,
            //    ssMng.TokenKey,
            //    ssMng.TokenUserTempData,
            //    false);

            #endregion
            
            
            string hostPart = Request.ServerVariables["HTTP_HOST"];
            string prefixPart = !string.IsNullOrEmpty(ConfigurationSettings.AppSettings["SubSystem"]) ? "/" + ConfigurationSettings.AppSettings["SubSystem"] : "";
            string strSystemPart = "";
            string homeUrlPart = "";
            //http://192.168.100.14/GBMS/App_UI/Forms/Default.aspx        *** prefixPart = "",         essPart = "/ESS"
            //http://192.168.137.1/KPISERP/GBMS/App_UI/Forms/Default.aspx *** prefixPart = "/KPISERP", essPart = "/ESS"
            //http://localhost:1234/App_UI/Forms/Default.aspx            *** prefixPart = "",         essPart = "" 

            switch (strSystem)
            { 
                case "APPROVAL":
                    homeUrlPart = "/Account/LoginByPass";
                    strSystemPart = "/OMVC";
                    break;
                case "GIMS":
                    homeUrlPart = "/Home/SingleSigonCookie";
                    strSystemPart = "/WEB.GIMS";
                    if (!Request.Url.IsDefaultPort)
                    {
                        // for testing only, if runing ess via visual studio, it redirect to web.gims visual studio
                        hostPart = "localhost:60830";
                    }
                    break;
                case "GBMS":
                    homeUrlPart = "/App_UI/Form/Default.aspx";
                    strSystemPart = "/GBMS";
                    break;
                default:
                    homeUrlPart = "/App_UI/Forms/Default.aspx";
                    strSystemPart = "/" + strSystem;
                    break;
            }
            strSystemPart = Request.Url.IsDefaultPort ? strSystemPart : string.Empty;
            strURL = string.Format("http://{0}{1}{2}{3}", hostPart, prefixPart, strSystemPart, homeUrlPart); 
            Response.Redirect(strURL);
        }
        
        protected void cmd_Command(object sender, CommandEventArgs e)
        {
            RedirectSystem(e.CommandArgument.ToString());
        }
    }
}
